package cn.naughtior.shch.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.naughtior.shch.dao.IUserDao;
import cn.naughtior.shch.entity.User;
import cn.naughtior.shch.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.naughtior.shch.exception.UserException;

@Service("userService")
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;

	@Override
	public int deleteUserByUuid(String uuid) throws UserException {
		if (uuid.isEmpty()) {
			throw new UserException("uuid不能为空！");
		}
		int result = 0;
		try {
			result = userDao.deleteUserByUuid(uuid);
		} catch (Exception e) {
			throw new UserException("删除用户出错！");
		}
		return result;
	}

	@Override
	public int updateUser(User user) throws UserException {
		if (user == null) {
			throw new UserException("用户 不能为空！");
		} 
		int result = 0;
		try {
			result = userDao.updateUser(user);
		} catch (Exception e) {
			throw new UserException("更新用户出错！");
		}
		return result;
	}

	@Override
	public User selectUserByMobilePhone(String mobilePhone) throws UserException {
		if (mobilePhone.isEmpty()) {
			throw new UserException("手机号码不能为空！");
		} 
		User user = new User();
		try {
			user = userDao.selectUserByMobilePhone(mobilePhone);
		} catch (Exception e) {
			throw new UserException("查询用户出错！");
		}
		return user;
	}

	@Override
	public User selectUserByUuid(String uuid) throws UserException {
		if(uuid == null) {
			return null;
		}
		if (uuid.isEmpty()) {
			throw new UserException("uuid不能为空！");
		} 
		User user = new User();
		try {
			user = userDao.selectUserByUuid(uuid);
		} catch (Exception e) {
			throw new UserException("查询用户出错！");
		}
		return user;
	}

	@Override
	public List<User> selectUserByName(String name) throws UserException {
		if (name.isEmpty()) {
			throw new UserException("姓名(name)不能为空！");
		} 
		List<User> list = new ArrayList<User>();
		try {
			list = userDao.selectUserByName(name);
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException("查询用户出错！");
		}
		return list;
	}

	@Override
	public User selectUserByUsername(String username) throws UserException {
		if (username.isEmpty()) {
			throw new UserException("用户名(username)不能为空！");
		} 
		User user = new User();
		try {
			user = userDao.selectUserByUsername(username);
		} catch (Exception e) {
			throw new UserException("查询用户出错！");
		}
		return user;
	}

}
